const app = getApp()
//获得全局js变量
var util = require('../../../../utils/util.js');
Page({
    data: {
        formData: {
            name: '',
            branchName: '',  // 开户行
            bankName: '',
            cardNumber: '',
            phone: ''
        },
        submitDisabled: true
    },
    
    onLoad: function() {
        // 获取用户基本信息
        const app = getApp();
        if (app.globalData.userInfo) {
            this.setData({
                'formData.name': app.globalData.userInfo.name || '',
                'formData.phone': app.globalData.userInfo.phone || ''
            });
        }
    },
    
    // 输入框内容变化
    inputChange: function(e) {
        const { field } = e.currentTarget.dataset;
        const { value } = e.detail;
        
        this.setData({
            [`formData.${field}`]: value
        });
        
        this.checkFormValid();
    },
    
    // 检查表单是否有效
    checkFormValid: function() {
        const { name, branchName, bankName, cardNumber, phone } = this.data.formData;
        const isValid = name && branchName && bankName && cardNumber && phone;
        
        this.setData({
            submitDisabled: !isValid
        });
    },
    
    // 提交表单
    submitForm: function() {
        if (this.data.submitDisabled) {
            return;
        }
        util.httpRrquest(app.data.rooturl + 'user/joinBankcard', 'POST',this.data.formData)
        .then(function (response) {
            if (response.data.code == 1) {
                wx.showModal({
                    title: '成功',
                    content: '您的银行卡添加成功',
                    showCancel: false,
                    success: () => {
                        wx.navigateBack();
                    }
                });	
            }
        }, function (error) {
            console.log(error);
        });
        
    }
})